package com.chanyachting.emall.action.newsactivity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.form.NewsActivityForm;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.service.NewsActivityService;
import com.chanyachting.emall.serviceImpl.NewsActivityServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;

public class CreateAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION,new Member());
	
		NewsActivityForm naForm=(NewsActivityForm)form;
		 
		 //上传文件，并返回文件web访问地址
		String imgUrl=MyFile.saveFileForm(naForm.getFile(), request);	
		
		Newsactivity na=new Newsactivity();
		
		BeanUtils.copyProperties(na, naForm);
		
		na.setImgUrl(imgUrl);
		na.setCreateDate(DateUtil.getDate());
		na.setMemberId(member.getId());
		na.setStatus("00");
		na.setRefreshDate(DateUtil.getDate());
		na.setReadNums(0);
		
		NewsActivityService naservice=new NewsActivityServiceImpl();
		
		//执行数据库插入 
		boolean temp=naservice.createNewsActivity(na);
		
		if(temp){
			
			ru.setMsg("创建成功！");
			
		}else{
			
			ru.setMsg("创建失败！请稍候重试。");
		}
		
		
		return mapping.findForward("create");
	}
	
	
	

}
